package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.StrategyCatalogQueryObject;
import cn.wolfcode.luowowo.common.query.StrategyThemeCopyQueryObject;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinatonService destinatonService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IRecommendService recommendService;
    @Reference
    private IStrategyThemeCopyService strategyThemeCopyService;
    @RequestMapping("")
    public String index(Model model){
        //查询所有的热门地区,共享到页面上
        List<Region> list = regionService.queryHotRegions();
        model.addAttribute("hotRegions",list);
        return "destination/index";
    }
    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,Long regionId){
        model.addAttribute("regionId",regionId);
        //1.根据regionId去destination表中查询数据
        List<Destination> list = destinatonService.queryDestByRegionId(regionId);
        //2.拼接数据块
        int size = list.size();
        //leftDests
        model.addAttribute("leftDests",list.subList(0,size/2 + 1));
        //rightDests
        model.addAttribute("rightDests",list.subList(size/2 + 1,size));
        //3.返回模板
        return "destination/hotdestTpl";

    }
    @RequestMapping("/guide")
    public String guide(Model model,Long id){
        List<Destination> toasts = destinatonService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        //根据destId,共享攻略过去
       List<StrategyCatalog> catalogs =  strategyCatalogService.getCatalogsByDestId(id);
       model.addAttribute("catalogs",catalogs);
       //共享strategyDetails 前三过去
        List<StrategyDetail> strategyDetails = strategyDetailService.getDetailsByDestId(id);
        model.addAttribute("strategyDetails",strategyDetails);
        return "destination/guide";
    }
    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQueryObject qo){
        //共享吐司过去
        List<Destination> toasts = destinatonService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQueryObject qo){
        //共享catalogs到模板
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogsByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);
        //根据catalogId获取catalog
        StrategyCatalog catalog = strategyCatalogService.getById(qo.getCatalogId());
        model.addAttribute("catalog",catalog);
        //detail,默认查询第一个
        if(catalog != null && catalog.getDetails() != null && catalog.getDetails().size()>0){
            //默认查询第一条
            StrategyDetail detail = catalog.getDetails().get(0);
            //将文章内容设置进去
            detail.setStrategyContent(strategyDetailService.getContentById(detail.getId()));
            //共享
            model.addAttribute("detail",detail);
        }
        return "destination/surveyTpl";
    }
    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo") TravelQueryObject qo){
        qo.setState(Travel.STATE_RELEASE);
        //共享pageInfo过去
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/travelTpl";
    }
    //推荐月份
    /*@RequestMapping("/month")
    @ResponseBody
    public Object month(Model model,@ModelAttribute("qo") RecommendQueryObject qo){
        List<Recommend> recommends = recommendService.selectAllRecommendsByMonth(qo);

        return recommends;
    }*/
    @RequestMapping("/filter")
    public String filter(Model model, @ModelAttribute("qo") StrategyThemeCopyQueryObject qo,Long themeId){
        //共享所有的出行方式到页面
        //themes
        List<StrategyThemeCopy> themesAll =  strategyThemeCopyService.selectAllThemesByType(qo);
        model.addAttribute("themesAll",themesAll);
        //themeSeason
        qo.setType("季节");
        List<StrategyThemeCopy> themeSeason =  strategyThemeCopyService.selectAllThemesByType(qo);
        model.addAttribute("themeSeason",themeSeason);
        //themeLeaveType
        qo.setType("出行方式");
        List<StrategyThemeCopy> themeLeaveType =  strategyThemeCopyService.selectAllThemesByType(qo);
        model.addAttribute("themeLeaveType",themeLeaveType);
        //根据主题id查询出推荐信息
        qo.setThemeId(themeId);
       List<Recommend> destination =  recommendService.selectAllByThemeId(qo);
       model.addAttribute("destination",destination);
        return "destination/travelFilter";
    }
    //根据主题id去查询
    @RequestMapping("/themeAndDays")
    public String themeAndDays(Model model, @ModelAttribute("qo") StrategyThemeCopyQueryObject qo){
        //根据主题id查询出推荐信息
        List<Recommend> filter =  recommendService.selectAllByThemeId(qo);
        model.addAttribute("filter",filter);
        return "destination/travelFilterTpl";
    }

}
